const JwtStrategy = require("passport-jwt").Strategy,
  ExtractJwt = require("passport-jwt").ExtractJwt;
const { DataSystemUserModel } = require("../models/sys_user")
const config = require("./../config");
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = config.secretOrkey;

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, async function (jwt_payload, done) {
      const user = await DataSystemUserModel.findBySystemUserId(jwt_payload.user_id);
      if (Array.isArray(user) && user.length) {
        return done(null, user[0]);
      } else {
        return done(null, false);
      }
    })
  );
};
